package com.lushu.chapitre1.graphics.containers
{
	import com.sfdk.graphics.core.SpriteEx;
    import com.sfdk.graphics.elements.GraphicElement;

    import flash.display.Bitmap;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;

	public class GraphicElementContainer extends SpriteEx
	{
		private var _graphicElement : GraphicElement;
		private var _containingBitmap : Bitmap;
		private var _mainLoopTimer : Timer;
		private var _width : int;
		private var _height : int;
		private var _align : String;
		
		public function GraphicElementContainer(graphicElement : GraphicElement = null, width : int = 0, height : int = 0, align : String = "center")
		{
			_width = width;
			_height = height;
			_align = align;
			
			_graphicElement = graphicElement;
			_containingBitmap = new Bitmap();
			attachChild(_containingBitmap, 0, 0, "containingBitmap");
			
			_mainLoopTimer = new Timer(20, 0);
			_mainLoopTimer.addEventListener(TimerEvent.TIMER, onLoop);
			_mainLoopTimer.start();
		}
		
		private function onLoop(e : TimerEvent) : void
		{
			if(_graphicElement.update(false))
				_containingBitmap.bitmapData = _graphicElement.view;
			if(_align == "center")
			{
				_graphicElement.x = 20;
				_graphicElement.y = 20;
				var position : Point = _graphicElement.position;
				_containingBitmap.x = (position.x)// + (_width  - _containingBitmap.width) >> 1 ;
				_containingBitmap.y = (position.y)// + (_height  - _containingBitmap.height) >> 1 ;
			}
		}
	}
}